import {getFault, getMaintainDetail, getMaintainList, getMaintainRecordDetail, postFault} from '@/base/api/ServiceApi';
import {isEmpty} from '@/base/util/Strings';
import {Page} from '@/base/Page';
import {getDeviceModel} from '@/base/api/DeviceModelApi';
import {deepAssign} from '@/base/util/Objects';
import {DeviceModel} from '@/base/model/DeviceModel';

class model1 {
  // eslint-disable-next-line camelcase
  FK_AllocateID = '0';
  PartName = '';
  Quantity = '';
  UnitPrice = '';
}
export class MaintainBloc {
  model: any = {
    address: '',
    name: '',
    phone: 0,
    images: '',
    video: '',
    PM: '', // 设备型号
    ProModel: '',
    buyTime: '', // 购买日期
    applyTime: '', // 申请时间
    SN: '', // 设备序列号
    state: '',
    description: '',
    id: '',
    aid: '',
    describe: '',
    ordernum: '',
    workname: '',
    allocateid: 0,
    Count: 0
  };
  model1: any = {
    Breakdown_Description: '',
    ConsigneeName: '',
    DealerName: '',
    FK_AllocateID: '0',
    IsUnderWarranty: '0',
    LaborCost: '0.00',
    MaintenanceName: '',
    MaintenanceTime: '',
    PartsCost: '0.00',
    PostSaleID: '0',
    PostSaleNum: '',
    Remark: '',
    SerialNum: '',
    State: '0'
  };
  model2:any = {
    Attitude: '0',
    Clothing: '0',
    Times: '0',
    Comment: '',
    FK_PostSaleID: '0',
    Speciality: '0',
    Star: '0',
    SubmitTime: '0'
  };
  paged1: Page<model1> = new Page<model1>([], 0, 1, 10, 0, []);

  loading = false;
  fileList: Array<any> = [];
  faultSelected= '';
  AddSelectOption='';
  num: number = 0;
  videoList: Array<any> = [];
  WatchList: Array<any> = [];
  // sortId: number = 0;
  // modelId: number = 0;
  isDetail = false;
  i = false;
  isAble = false;
  hasFormInfo = false;

  fileListRecord: Array<any> = [];
  fileListRecord2: Array<any> = [];
  videoListRecord: Array<any> = [];
  hasFormInfoRecord = 0;
  hasFormInfoRecord2 = 0;
  numRecord1: number = 1;
  numRecord2: number = 1;

  page1: Page<DeviceModel> = new Page<DeviceModel>([], 0, 1, 15, 0, []);
  modelList: any = [];
  OriginPM = '';
  OriginSN = '';

  Editsymbol = false;

  load(id: number, aid: number) {
    return getMaintainDetail({id, aid})
      .then((res) => {
        // @ts-ignore
        let json = JSON.parse(res.data);
        if (json.errMsg.repairer) {
          this.model.workname = json.errMsg.repairer.name;
        }
        this.model.address = json.errMsg.address.address;
        this.model.name = json.errMsg.address.name;
        this.model.phone = json.errMsg.address.phone;
        this.model.images = json.errMsg.images;
        this.model.video = json.errMsg.video;
        if (!isEmpty(this.model.images)) {
          this.model.images.map(x => this.fileList.push(x));
        }
        if (!isEmpty(this.model.video)) {
          this.model.video.map(x => this.videoList.push(x));
        }
        return res;
      }).finally(() => {
        this.hasFormInfo = true;
      })
  }

  loadRecord(id: number) {
    this.loading = true;
    return getMaintainRecordDetail({id})
      .then((res) => {
        // eslint-disable-next-line no-eval
        let data = eval('(' + res.data + ')');
        if (!isEmpty(data.errMsg.Detail)) {
          this.model1 = data.errMsg.Detail;
        } else {
          this.numRecord1 = 0;
        }
        if (!isEmpty(data.errMsg.Comment)) {
          this.model2 = data.errMsg.Comment;
        } else {
          this.numRecord2 = 0;
        }
        this.paged1.items = data.errMsg.Results;
        if (!isEmpty(data.errMsg.Picture)) {
          Object.entries(data.errMsg.Picture).forEach(
            ([key, value]) => {
              let name = this.returnName(key);
              if (!isEmpty(value) && name !== 'workVideo') {
                // @ts-ignore
                this.fileListRecord.push([name, 'https://dd.irestapp.com/order/backend/web/imgOrigin/' + value[0]])
              } else if (!isEmpty(value) && name === 'workVideo') {
                // @ts-ignore
                this.videoListRecord.push(['维修视频', 'https://dd.irestapp.com/order/backend/web/imgOrigin/' + value[0]])
              }
            }
          );
        } else {
          this.hasFormInfoRecord += 1;
        }
        if (!isEmpty(data.errMsg.CommentPicture)) {
          data.errMsg.CommentPicture.map(x => this.fileListRecord2.push('https://dd.irestapp.com/order/backend/web/imgOrigin/' + x.PictureUrl));
        } else {
          this.hasFormInfoRecord2 += 1;
        }
      }).finally(() => {
        this.loading = false;
      })
  }
  getAllModel(pageIndex: number, pageSize: number, sortId, sortDirection: string) {
    this.loading = true;
    let that = this;
    return getDeviceModel({
      pageIndex,
      pageSize,
      sortId,
      sortDirection,
      companyId: undefined,
      id: undefined,
      modelName: ''
    }).then((res) => {
      deepAssign(this.page1, res.data);
      this.modelList = this.page1.items.map(function (item) {
        var obj = { id: 0, modelName: '' };
        obj.id = item.id;
        obj.modelName = item.modelName;
        return obj;
      });
      return this.modelList;
    }).finally(() => {
      this.loading = false;
    });
  }
  returnName(name) {
    switch (name) {
      case 'ensure': return '服务确认单';
      case 'broken': return '产品故障位置';
      case 'serial': return '产品序列号';
      case 'resolve': return '解决结果';
      case 'recover': return '复原后全景';
    }
  }
  shuchu(value) {
    let rating = '';
    for (let i = 0; i < value; i++) {
      rating += '★';
    }
    for (let a = 0; a < 5 - value; a++) {
      rating += '☆';
    }
    return rating;
  }
  save(id, str, Addstr, num) {
    this.loading = true;
    return postFault({
      id: id,
      faultId: str,
      faultInput: Addstr,
      sn: this.model.SN,
      pm: this.model.PM,
      allType: num
    }).then((res) => {
      return res;
    }).finally(() => {
      this.num = 0;
      this.loading = false;
    });
  }

}
